package com.hfzy.ihk.web.ivr.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by Administrator on 2019/6/5.
 */
@Configuration
public class TaskExecutorConfig {

    /**
     * 收到识别以后推送到前端线程池，各线程池互补影响防止某一个服务雪崩阻塞其他服务，队列满了抛异常
     * @return
     */
    @Bean
    public Executor pushASRDataExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("ivr pushASRDataExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }

    /**
     * 推送到ES线程池，队列满了抛异常
     * @return
     */
    @Bean("pushDataToEsTaskExecutor")
    public Executor pushDataToEsTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("ivr pushDataToEsTaskExecutor-");

        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }

    /**
     * 推送到分析服务线程池，队列满了抛异常
     * @return
     */
    @Bean
    public Executor pushDataToAnalysisTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("ivr pushDataToAnalysisTaskExecutor-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }

    /**
     * 推送到置业线程池
     * @return
     */
    @Bean
    public Executor pushDataToOracleTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//默认10个线程，
        executor.setMaxPoolSize(20); //队列满了再加10个
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("pushDataToOracleTaskExecutor-");

        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        return executor;
    }
}
